package cn.xo68.ide.plugin.common.ui.toolwindow;


import cn.xo68.ide.plugin.common.action.ToolSuiteLogCleanConsoleAction;
import cn.xo68.ide.plugin.common.component.ToolSuiteConsole;
import cn.xo68.ide.plugin.common.message.StatusListener;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.tools.SimpleActionGroup;
import com.intellij.util.messages.MessageBusConnection;
import icons.WxIcons;

import javax.swing.*;


public class ToolSuiteLogPanel extends SimpleToolWindowPanel {
  private static final String ID = "WxToolSuite";

  private final ToolWindow toolWindow;
  private final Project project;

  private ActionToolbar mainToolbar;

  public ToolSuiteLogPanel(ToolWindow toolWindow, Project project) {
    super(false, true);
    this.toolWindow = toolWindow;
    this.project = project;

    addToolbar();
    addConsole();

    MessageBusConnection busConnection = project.getMessageBus().connect(project);
    busConnection.subscribe(StatusListener.STATUS_TOPIC, newStatus ->
      ApplicationManager.getApplication().invokeLater(mainToolbar::updateActionsImmediately));
  }

  private void addToolbar() {
    ActionGroup actionGroup = createActionGroup();
    mainToolbar = ActionManager.getInstance().createActionToolbar(ID, actionGroup, false);
    mainToolbar.setTargetComponent(this);
    Box toolBarBox = Box.createHorizontalBox();
    toolBarBox.add(mainToolbar.getComponent());

    super.setToolbar(toolBarBox);
    mainToolbar.getComponent().setVisible(true);
  }

  private static ActionGroup createActionGroup() {
    SimpleActionGroup actionGroup = new SimpleActionGroup();
    ToolSuiteLogCleanConsoleAction toolSuiteLogCleanConsoleAction=new ToolSuiteLogCleanConsoleAction("","", WxIcons.CLEAN);
    actionGroup.add(toolSuiteLogCleanConsoleAction);
    return actionGroup;
  }



  private void addConsole() {
    ConsoleView consoleView = this.project.getComponent(ToolSuiteConsole.class).getConsoleView();
    super.setContent(consoleView.getComponent());
  }
}
